<?php
namespace Weibo\Controller;
use Common\Controller\AdminbaseController;

/**
 * 举报管理分类控制器
 * @author Hongwei	2016-12-26
 */
class AdminReportcateController extends AdminbaseController{

	public function _initialize() {

	}

	/**
	 * 举报分类首页
	 */
	public function index(){
		$cate_model = M('WeiboReportcate');
		$list = $cate_model
			-> order(array('orderby'=>'asc'))
			-> select();
		$this -> assign('list', $list);
		$this->display();
	}

	/**
	 * 举报分分类列表的批量操作
	 * @param string $type	处理类型 order排序
	 */
	public function indexBat($type='order') {

		if ($type == 'order') {
			$order_array = I('post.orderby');

			$cate_model = D('WeiboReportcate');
			if ($cate_model -> saveBatch($order_array, 'cate_id', 'orderby') !== FALSE) {
				$this -> success('排序成功');
			} else {
				$this -> error('排序出错');
			}
		}
	}
	/**
	 * 举报分类添加
	 */
	public function add() {
		$this -> _modify();
	}


	/**
	 * 举报分类编辑
	 */
	public function edit(){
		$this -> _modify();
	}

	/**
	 * 举报分类删除
	 */
	public function delete(){
		$model = M('WeiboReportcate');
		$cate_id = I('cate_id');
		$result = $model -> delete($cate_id);

		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$cate_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);
	}
	/**
	 * 修改公用方法(只有一级分类)
	 */
	private function _modify() {
		$cate_model = M('WeiboReportcate');	//pay attention
		$cate_id = I('cate_id');

		if (IS_POST) {
			//根据需要增加减少
			$validate = array(
				array('cate_name', 'require', '分类名不能为空'),
			);
			$result = array('status' => TRUE, 'info' => '', 'url' => ''); //这个什么意思TODO

			$cate_model -> field('cate_name,orderby');	//pay attention

			if ($cate_model -> validate($validate) -> create()) {
				if (!$cate_id) {
					$insert_id = $result =$cate_model -> add();
					if ($insert_id) {
						$this -> success('添加成功');
					} else {
						$this -> success('添加失败');
					}
				} else {
					if($cate_model -> where(array('cate_id' => $cate_id)) -> save()) {
						$this -> success('修改成功');
					} else {
						$result['status'] = FALSE;
						$this -> success('修改失败');
					}
				}
			} else {
				//返回错误提示
				$result['status'] = FALSE;
				$result['info'] = $cate_model -> getError();
			}

			if ($result['status']) {
				$this -> success($result['info']);
			} else {
				$this -> error($result['info']);
			}
		}

		$cate = $cate_id ? $cate_model -> find($cate_id) : array();

		$this -> assign('cate', $cate);
		$this -> display(':AdminReportcate/form');	//pay attention
	}

}